﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using Data.Model;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using Microsoft.Practices.Prism.ViewModel;
using ServiceModel.Operations;
using ServiceStack.Service;
using ServiceStack.ServiceClient.Web;

namespace UI.Module.Admin
{
    public class UsersViewModel : NotificationObject
    {
        public IServiceClient ServiceClient { get; private set; }

        public UsersViewModel()
        {
            this.SelectedUserRequest = new InteractionRequest<Notification>();
            _editCommand = new DelegateCommand(OnEdit, CanEdit);
            IsBusy = true;
            GetData();
        }

        private void GetData()
        {
            ServiceClient = new JsonServiceClient("http://pc03:3010/")
            {
                StoreCookies = false,
                Timeout = new TimeSpan(0, 5, 0),
            };
            var securityContext = new SecurityContext(ServiceClient);

            IsBusy = true;
            Users = new ObservableCollection<UserBase>();
            securityContext.GetUsers(obj => OnGetUsersSucceed(obj), (res, ex) => OnError(ex));
        }

        private void OnError(Exception ex)
        {
            IsBusy = false;
            Console.WriteLine(ex.Message);
        }
        private void OnGetUsersSucceed(GetUsersResponse response)
        {
            if (string.IsNullOrWhiteSpace(response.ResponseStatus.Message))
            {
                this.Users = new ObservableCollection<UserBase>(response.Users);
            }
            IsBusy = false;
        }

        private ObservableCollection<UserBase> _users;
        public ObservableCollection<UserBase> Users
        {
            get { return _users; }
            set
            {
                if (value == _users) return;
                _users = value;

                RaisePropertyChanged(() => Users);
            }
        }

        private bool _isBusy;
        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                IsLoaded = !value;
                RaisePropertyChanged(() => IsBusy);
            }
        }

        private bool _isLoaded;
        public bool IsLoaded
        {
            get { return _isLoaded; }
            set
            {
                _isLoaded = value;
                RaisePropertyChanged(() => IsLoaded);
            }
        }

        private UserBase _selecteduser;
        public UserBase SelectedUser
        {
            get { return _selecteduser; }
            set
            {
                _selecteduser = value;
                RaisePropertyChanged(() => SelectedUser);
            }
        }

        public DelegateCommand EditCommand { get { return _editCommand; } }
        private readonly DelegateCommand _editCommand;
        private bool CanEdit()
        {
            return true;
        }
        private void OnEdit()
        {
            SelectedUserRequest.Raise(new Notification{Title = "User"});
        }

        public InteractionRequest<Notification> SelectedUserRequest { get; private set; }
    }
}
